package com.itheima.controller;


import com.itheima.pojo.Admin;
import com.itheima.pojo.AdminVO;
import com.itheima.pojo.Result;
import com.itheima.pojo.StatusCode;
import com.itheima.service.AdminService;
import com.itheima.utils.CookieUtils;
import com.itheima.utils.TokenUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/auth")
public class AuthController {

   //1 获取当前用户的菜单  GET /auth/getMenu  // 用户退出，没有token  {"success": 0,"resultCode": "00001","message": "操作失败！"}

    @Autowired
    private AdminService adminService;

    @GetMapping("/getMenu")
    public  Map getMenu(HttpServletRequest request){
         Map<String, Object> userInfo = TokenUtil.getUserInfo(request);
        try {
            Map map= adminService.getMenu(userInfo);
            Map<String, Object> result1 = new HashMap<>();
            result1.put("code",StatusCode.OK);
            result1.put("data",map);
            return result1;
        } catch (Exception e) {
            e.printStackTrace();
        }

        Map<String, Object> result2 = new HashMap<>();
        result2.put("success",0);
        result2.put("resultCode","00001");
        result2.put("message","操作失败！");
        return result2;
    }


    //2  GET /auth/info ( String token)  用户信息  {"code":"error","data":"token用户过期"}
    //获取管理员角色信息
    @GetMapping("/info")
    public  Result info(HttpServletRequest request){



        Map<String, Object> userInfo;
        try {
            userInfo = TokenUtil.getUserInfo(request);
            String username= (String)userInfo.get("username");
            Map<String, Object> map = adminService.info(username,request);

          return new Result(StatusCode.OK,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,null);
        }
        //查询管理员对应的角色数据和管理员图片数据，令牌数据
        //{"code":"success","data":{"roles":[{"roleName":"超级管理员","summary":"超级管理员，管理全部菜单和功能","categoryMenuUids":"[\"49b42250abcb47ff876bad699cf34f03\",\"1f01cd1d2f474743b241d74008b12333\",\"fe45ea293f75dc88b96cab96c218512a\",\"e5218b0b1cf016ae21eced4e47b628ac\",\"0cab1fcdcd01f394768e2e2674e56773\",\"16a75a3c38e71c1046b443b4b64dd930\",\"2b983c5439ac07f2cf07437ba9fff6be\",\"7ada0a7053be4ad4b31f6be64315d03a\",\"9db7ffbded9717f13a1a97fca46bc17c\",\"0a035547bbec404eb3ee0ef43312148d\",\"2a733ff390af9b44ecda4e8c4634d75d\",\"327d945daf4ddb71976c4ab3830e7c66\",\"6c8a8c50c77429fab210bd52ed8c50bb\",\"72d26cf940bf9dfb6bde0a590ff40882\",\"80ee135af885f02d52ecb67d5a05b173\",\"aef85c40b54320d7c5a9d78697e12910\",\"f87d2f9b4539abbade38583420dc8b89\",\"fbc30e4ae5bb33b39baca7bf6bd8ce99\",\"78ab104b123f4950af14d65798afb756\",\"6606b7e646d545e5a25c70b5e5fade9f\",\"2a733ff390af9b44ecda4e8c4634d75f\",\"327d945daf4ddb71976c4ab3830e7c6h\",\"6c8a8c50c77429fab210bd52ed8c50ba\",\"72d26cf940bf9dfb6bde0a590ff4088g\",\"80ee135af885f02d52ecb67d5a05b17b\",\"aef85c40b54320d7c5a9d78697e1291c\",\"f87d2f9b4539abbade38583420dc8b8e\",\"fbc30e4ae5bb33b39baca7bf6bd8ce9d\",\"1d9a5030142e9fd7690f554c20e3bc90\",\"f874529321a37e84f099488a4eaf5a0b\",\"badf0010422b432ba6ec9c83a25012ed\",\"faccfe476b89483791c05019ad5b4906\",\"2a733ff390af9b44ecda4e8c4634dm8k\",\"f87d2f9b4539abbade38583420dc8l9l\",\"fbc30e4ae5bb33b39baca7bf6bd8ck0m\",\"c28f0b052e0b930299dd53de59cc32d7\",\"2a733ff390af9b44ecda4e8c4634dh8k\",\"327d945daf4ddb71976c4ab3830e7g7i\",\"f87d2f9b4539abbade38583420dc8i9l\",\"fbc30e4ae5bb33b39baca7bf6bd8cj0m\",\"9002d1ae905c4cb79c2a485333dad2f7\",\"2a733ff390af9b44ecda4e8c4634d78k\",\"327d945daf4ddb71976c4ab3830e7c7i\",\"aef85c40b54320d7c5a9d78697e1296n\",\"f87d2f9b4539abbade38583420dc8b9l\",\"fbc30e4ae5bb33b39baca7bf6bd8ce0m\",\"78f24799307cb63bc3759413dadf4d1a\",\"587e2697fa4d85046feece8ab9d0706c\",\"f87d2f9b4539abbade38583420dc8byl\",\"fbc30e4ae5bb33b39baca7bf6bd8cezm\",\"2fb47d3b6dbd44279c8206740a263543\",\"f87d2f9b4539abbade38583420dc8bpl\",\"fbc30e4ae5bb33b39baca7bf6bd8ceom\",\"26bcccf0164bf84f12ab20448388d346\",\"62c91c47c4646661c045727f0a8eb9d4\",\"7be9dee2f91165d8562aaa80c7e5b936\",\"9e73a9b1de18e4f1cb19eca5e8cd321d\",\"ab1289c29b336dccda87a9fa8b711aa2\",\"e91945e49c5fdb207f996bc9668cd965\",\"062087bce19d00312b3787b6e24c21d1\",\"2a733ff390af9b44ecda4e8c4634d7ck\",\"327d945daf4ddb71976c4ab3830e7cdi\",\"f87d2f9b4539abbade38583420dc8bbl\",\"fbc30e4ae5bb33b39baca7bf6bd8ceam\",\"d3a19221259d439b916f475e43edb13d\",\"aa225cdae6464bc0acebd732192f8362\",\"2a733ff390af9b44ecda4e8c4634d73k\",\"327d945daf4ddb71976c4ab3830e7c4i\",\"aef85c40b54320d7c5a9d78697e1295n\",\"f87d2f9b4539abbade38583420dc8b2l\",\"fbc30e4ae5bb33b39baca7bf6bd8ce1m\",\"879cfcd4dfd3e5bc1bb6ea2c0f1f82c0\",\"5010ae46511e4c0b9f30d1c63ad3f0c1\",\"2a733ff390af9b44ecda4e8c4634d7uk\",\"327d945daf4ddb71976c4ab3830e7cvi\",\"f87d2f9b4539abbade38583420dc8btl\",\"fbc30e4ae5bb33b39baca7bf6bd8cesm\",\"2de247af3b0a459095e937d7ab9f5864\",\"2a733ff390af9b44ecda4e8c4634d7lk\",\"327d945daf4ddb71976c4ab3830e7cmi\",\"72d26cf940bf9dfb6bde0a590ff408nj\",\"f87d2f9b4539abbade38583420dc8bkl\",\"fbc30e4ae5bb33b39baca7bf6bd8cejm\",\"f4697cdf85920369179b90ff45a5982d\",\"f3a559635f9d46ee3356d072f5896fcb\",\"672b093002b618293befd8e1a164e4cc\",\"407a263eb12eff5aac31e9f62901cea0\",\"34c1b7b1bd3118979fe1f9f9a1147fa5\",\"c519725da92b42f3acf0cc9fad58c664\",\"fb4237a353d0418ab42c748b7c1d64c6\",\"2a733ff390af9b44ecda4e8c4634d75t\",\"72d26cf940bf9dfb6bde0a590ff4088s\",\"f87d2f9b4539abbade38583420dc8b8r\",\"fbc30e4ae5bb33b39baca7bf6bd8ce9q\",\"b511cae571834971a392ae4779270034\",\"98b82be8785e41dc939b6a5517fdfa53\",\"a5902692a3ed4fd794895bf634f97b8e\",\"fbc30e4ae5bb33b39baca7bf6bd8celm\",\"9e91b4f993c946cba4bf720b2c1b2e90\",\"fbc30e4ae5bb33b39baca7bf6bd8cemm\",\"4337f63d13d84b9aba64b9d7a69fd066\",\"fbc30e4ae5bb33b39baca7bf6bd8cekm\",\"bcf4a9bc21c14b559bcb015fb7912266\",\"9beb7caa2c844b36a02789262dc76fbe\",\"fbc30e4ae5bb33b39baca7bf6bd8ce9m\",\"2a733ff390af9b44ecda4e8c4634d75k\",\"327d945daf4ddb71976c4ab3830e7c6i\",\"72d26cf940bf9dfb6bde0a590ff4088j\",\"f87d2f9b4539abbade38583420dc8b8l\",\"6228ff4e9ebd42c89599b322201a0345\",\"9c91231f1682aabd765225a7e503bb43\",\"f87d2f9b4539abbade38583420dc8bxl\",\"fbc30e4ae5bb33b39baca7bf6bd8cewm\",\"baace3dc03d34c54b81761dce8243814\",\"f9276eb8e3274c8aa05577c86e4dc8c1\",\"e4a482c089d04a30b6ecbaadb81b70f8\",\"d4d92c53d3614d00865e9219b8292a90\",\"acbb5d09da25e6c9e019cc361b35d159\",\"65e22f3d36d94bcea47478aba02895a1\",\"4dea9c4f39d2480983e8c4333d35e036\",\"2a733ff390af9b44ecda4e8c4634d7pk\",\"327d945daf4ddb71976c4ab3830e7cqi\",\"aef85c40b54320d7c5a9d78697e129rn\",\"f87d2f9b4539abbade38583420dc8bol\",\"fbc30e4ae5bb33b39baca7bf6bd8cenm\",\"1cc493d36e17fad535f2bf70242162b0\",\"2a733ff390af9b44ecda4e8c4634d7gk\",\"327d945daf4ddb71976c4ab3830e7cfi\",\"72d26cf940bf9dfb6bde0a590ff408ej\",\"f87d2f9b4539abbade38583420dc8bhl\",\"fbc30e4ae5bb33b39baca7bf6bd8ceim\",\"510483ce569b4fc88299f346147b1314\",\"ffc6e9ca2cc243febf6d2f476b849163\",\"2a733ff390af9b44ecda4e8c4634dh8c\",\"327d945daf4ddb71976c4ab3830e7g7d\",\"f87d2f9b4539abbade38583420dc8i9b\",\"fbc30e4ae5bb33b39baca7bf6bd8cj0a\",\"e1e54aea65cc22d9f8a4c74ce8d23749\",\"4062db4658392e71c177758bb51bb4cb\",\"5ef38a4f70c6d859155528776a30cda7\",\"688beda820a236b50f18ccdd5dc9fee9\",\"94bee9c48a4611c96b89112b845c7f28\",\"baa21ccb45ee133b064187185edb2ac0\",\"9449ce5dd5e24b21a9d15f806cb36e87\",\"2a733ff390af9b44ecda4e8c4634db8k\",\"327d945daf4ddb71976c4ab3830e7d7i\",\"aef85c40b54320d7c5a9d78697e12e6n\",\"f87d2f9b4539abbade38583420dc8c9l\",\"fbc30e4ae5bb33b39baca7bf6bd8ca0m\",\"147cd431cbb9007bde87444d7987b151\",\"bfcb9b002c3de18f269189c573b985f8\",\"2d5cd8a387fc2d010c245acd65fefd3b\",\"99f5d4233d446a809b9937961c590766\",\"cbd7ba11c1b38c66b569405ed9185f35\",\"bfc9463e59a3ca250dcfc1c86627e034\",\"2a733ff390af9b44ecda4e8c4634d75u\",\"a9396f1a3fbdec3d4cb614f388a22bea\",\"a0c40ddd3fe15bb3f2edf9ec242a1511\",\"14bcfc15d02f0d568adf2d383c0342d8\",\"93f7fd9a6e81735c47649e6b36042b5d\",\"505b4769b77617a314a3ed78e4acdff7\",\"079f0cfdb7a7017d827f5c349983eebc\",\"02ea2f9ef5d44f559fb66189b05f6769\",\"2a733ff390af9b44ecda4e8c4634d75v\"]","uid":"434994947c5a4ee3a710cd277357c7c3","status":1,"createTime":"2018-10-16 07:56:26","updateTime":"2020-06-15 10:39:59"}],"avatar":"http://localhost:8600//blog/admin/jpg/2020/2/24/1582507546061.jpg","token":"bearer_eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhZG1pblVpZCI6IjFmMDFjZDFkMmY0NzQ3NDNiMjQxZDc0MDA4YjEyMzMzIiwicm9sZSI6Im51bGzotoXnuqfnrqHnkIYiLCJjcmVhdFRpbWUiOjE1OTY4ODI3MjQ5NTEsInN1YiI6ImFkbWluIiwiaXNzIjoibW9ndWJsb2ciLCJhdWQiOiIwOThmNmJjZDQ2MjFkMzczY2FkZTRlODMyNjI3YjRmNiIsImV4cCI6MTU5NzE0MTkyNCwibmJmIjoxNTk2ODgyNzI0fQ.N1fRLPcsVJgW2OV-yVLNpXO1Oe2zXIQfNHUw8a4o6m0"}}
        //解析token 获取用户名
        // {"roles":[{"roleName":"","summary":"","categoryMenuUids":"","uid":"434994947c5a4ee3a710cd277357c7c3","status":1,"createTime":"2018-10-16 07:56:26","updateTime":"2020-06-15 10:39:59"}],"avatar":"http://localhost:8600//blog/admin/jpg/2020/2/24/1582507546061.jpg","token":""}
    }


    //3. POST /auth/login  (username,password,isRememberMe)    管理员用户登录

    @PostMapping("/login")
    public  Result login(@RequestParam Map map,HttpServletRequest request){
        Result result=null;
        try {
           result=adminService.login(map,request);
          if (result.getCode().equals("success")){
              return result;
          }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    //4. POST /auth/logout  退出登录  {"code":"success","data":"操作成功"}
    @PostMapping ("/logout")
    public  Result logout(HttpServletRequest request){

        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if ("Authorization".equals(name)){
                CookieUtils.deleteCookie(name);
            }
        }
        //用户退出， 删除token
        return new Result(StatusCode.OK,"退出成功");
    }


    //5.注册管理员  POST /auth/register
    @PostMapping ("/register")
    public  Result register(@RequestBody AdminVO adminVO,HttpServletRequest request){

        Admin admin = adminService.findAdmin1ByName(adminVO.getUserName());
        if (admin!=null){
            return new Result(StatusCode.ERROR,"该用户名已存在");
        }
        try {
            int add = adminService.add(adminVO,request);
            if (add>0){
                return new Result(StatusCode.ERROR,"注册成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(StatusCode.ERROR,"注册失败");
    }


    //6. 更新管理员密码  POST /auth/updatePassWord (管理员账户名userInfo,管理员旧密码passWord,
    //管理员新密码 newPassWord)

    @PostMapping ("/updatePassWord")
    public  Result updatePassWord(@RequestBody Map map){
        //查询俩次判断管理员是否存在和旧密码是否正确 ，再修改管理员密码
        //{"code":"error","data":"管理员不存在"}
       Admin admin1= adminService.findAdmin1ByName((String)map.get("userInfo"));
       if (admin1==null){
           return new Result(StatusCode.ERROR,"管理员不存在");
       }
       Boolean flag= adminService.findAdmin2BypassWord((String)map.get("passWord"),admin1);
       if (!flag){
           return new Result(StatusCode.ERROR,"旧密码错误");
       }
        //{"code":"error","data":"旧密码错误"}
        int i = adminService.updatePassWord(map);
        if (i>0){
            return new Result(StatusCode.OK,"修改成功");
        }
        return new Result(StatusCode.ERROR,"修改失败");
    }
}
